package com.itheima.jobs;

import cn.hutool.core.collection.CollectionUtil;
import com.itheima.constant.RedisConstant;
import com.itheima.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.Date;
import java.util.Set;

/**
 * @author: libin
 * @date: 2019/11/23 20:28
 * @version: 1.0
 * @describe: 定时清理图片任务
 */
@Component
public class ClearImages {

    @Autowired
    private JedisPool jedisPool;

    public void run() {
        Jedis resource = jedisPool.getResource();//set 有比较方法sdiff
        Set<String> sdiff = resource.sdiff(RedisConstant.SETMEAL_PIC_RESOURCES, RedisConstant.SETMEAL_PIC_DB_RESOURCES);//内容多的放前面

        if (CollectionUtil.isNotEmpty(sdiff)) {
            for (String imageName : sdiff) {
                //删除七牛里的图片
                QiniuUtils.delete(imageName);
                //删除SETMEAL_PIC_RESOURCES库中的存储信息
                resource.srem(RedisConstant.SETMEAL_PIC_RESOURCES,imageName);
            }
        }

    }
}
